package com.lfg.leetcode.leetcode200;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class No284PeekingIterator {


    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        PeekingIterator iter = new PeekingIterator(list.iterator());
        System.out.println(iter.next());
        System.out.println(iter.peek());
        System.out.println(iter.next());
        System.out.println(iter.hasNext());
    }

}


class PeekingIterator implements Iterator<Integer> {
    List<Integer> list = null;
    int index = 0;

    public PeekingIterator(Iterator<Integer> iterator) {
        list = new ArrayList<Integer>();
        if (null == iterator) {
            return;
        }
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            list.add(next);
        }
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        if (null == list || list.size() == 0) {
            return null;
        }
        return list.get(index);
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        if (null == list || list.size() == 0) {
            return null;
        }
        return list.get(index++);

    }

    @Override
    public boolean hasNext() {
        if (null == list || list.size() == 0) {
            return false;
        }
        return index < list.size();
    }
}
